package io.yuanqi.hgtcshop.controller;

import com.alibaba.fastjson.JSON;
import io.yuanqi.hgtcshop.entity.dos.MemberAddress;
import io.yuanqi.hgtcshop.entity.vo.ResultMessage;
import io.yuanqi.hgtcshop.service.MemberAddressService;
import io.yuanqi.hgtcshop.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * UserController {@link }
 *
 * @author: J.S
 * @since: 2024/1/26
 */
@Slf4j
@RestController
@RequestMapping("/buyer/member/address")
public class MemberAddressController {


    /**
     * 会员收件地址
     */
    @Autowired
    private MemberAddressService memberAddressService;



//    //  @ApiOperation(value = "获取会员收件地址分页列表")
    @GetMapping
    public ResultMessage<List<MemberAddress>> getAddressByMember(String openId) {
        log.info(".getAddressByMember openId:{}",openId);
        return ResultUtil.data(memberAddressService.getAddressByMember(openId));
    }

    //  @ApiOperation(value = "根据ID获取会员收件地址")
    //@ApiImplicitParam(name = "id", value = "会员地址ID", dataType = "String", paramType = "path")
    @GetMapping(value = "/get/{id}")
    public ResultMessage<MemberAddress> getShippingAddress(@PathVariable String id) {
        return ResultUtil.data(memberAddressService.getMemberAddress(id));
    }

    //  @ApiOperation(value = "获取当前会员默认收件地址")
    @GetMapping(value = "/get/default")
    public ResultMessage<MemberAddress> getDefaultShippingAddress(String openId) {
        return ResultUtil.data(memberAddressService.getDefaultMemberAddress(openId));
    }

    //  @ApiOperation(value = "新增会员收件地址")
    @PostMapping
    public ResultMessage<MemberAddress> addShippingAddress(MemberAddress shippingAddress) { //@RequestBody
        //添加会员地址
        shippingAddress.setMemberId(shippingAddress.getMemberId());
        if(shippingAddress.getIsDefault()==null){
            shippingAddress.setIsDefault(false);
        }
        shippingAddress.setIsDefault(true);
        return ResultUtil.data(memberAddressService.saveMemberAddress(shippingAddress));
    }

    //  @ApiOperation(value = "修改会员收件地址")
    @PostMapping("/edit")
    public ResultMessage<MemberAddress> editShippingAddress(MemberAddress memberAddress) {
        log.info(".editShippingAddress memberAddress:{}", JSON.toJSONString(memberAddress));
        memberAddress.setMemberId(memberAddress.getMemberId());
        return ResultUtil.data(memberAddressService.updateMemberAddress(memberAddress));
    }

    //  @ApiOperation(value = "删除会员收件地址")
//    @ApiImplicitParam(name = "id", value = "会员地址ID", dataType = "String", paramType = "path")
    @DeleteMapping(value = "/delById/{id}")
    public ResultMessage<Object> delShippingAddressById(@PathVariable String id) {
        memberAddressService.removeMemberAddress(id);
        return ResultUtil.success();
    }


}
